enemy = {}

enemy.pos = { x=0,y=0,gx=0,gy=0 }
--Image to draw for the enemy
enemy.image = love.graphics.newImage('game/sub1.png')
--Whether or not the enemy should move in any direction

--Max speed the enemy can move
enemy.maxVel = 150
--Whether or not to flip the enemy image to match their direction
enemy.drawL = true

--Missiles
enemy.missiles = {}
enemy.expMissile = love.sound.newSoundData('game/explodeMissile.ogg')
enemy.mi = love.graphics.newImage('game/missile.png')

function enemy:create()
  enemy.fire = false
  enemy.dead = false
  enemy.health = 3
  enemy.canFire = true
  enemy.move = {u=false,d=false,l=false,r=false}
  --Offset for drawing enemy
  enemy.iOff = {x=12 - grid, y = grid/2 - enemy.image:getHeight()/2 - 7 - grid}
  --Get the enemy's pixel position
  enemy.pos.x,enemy.pos.y = enemy.pos.gx*grid,enemy.pos.gy*grid
  --Create physics body
  enemy.body = love.physics.newBody(world, enemy.pos.x, enemy.pos.y, 'dynamic')
  enemy.body:setFixedRotation(true)
  enemy.body:setLinearDamping(0.1)
  enemy.shape = love.physics.newRectangleShape(0,-7.5*screen.scale,142.5*screen.scale, 54*screen.scale)
  enemy.fixture = love.physics.newFixture(enemy.body,enemy.shape)
  enemy.fixture:setRestitution(0.1)
  enemy.fixture:setCategory(3)
  
  --Coorinates for vector drawing of the enemy
enemy.vectorR = {[1] = {x = 0, y = 0}, [2] = {x = 28, y = -15}, [3] = {x = 138, y = -15}, [4] = {x = 145, y = -4},
      [5] = {x = 145, y = 8}, [6] = {x = 138, y = 19}, [7] = {x = 28, y = 19}, [8] = {x = 5, y = -4}, [9] = {x = 5, y = -15},
      [10] = {x = 5, y = -15}, [11] = {x = 26, y = -15}, [12] = {x = 5, y = 4}, [13] = {x = 5, y = 19},
      [14] = {x = 5, y = 19}, [15] = {x = 27, y = 19}, [16] = {x = 80, y = -16}, [17] = {x = 100, y = -35},
      [18] = {x = 100, y = -35},[19] = {x = 125, y = -35}, [20] = {x = 125, y = -15}, [21] = {x = 125, y = -35}}
enemy.vectorL = {[1] = {x = 145, y = 0}, [2] = {x = 117, y= -15}, [3] = {x = 7, y = -15}, [4] = {x = 0, y = -4},
    [5] = {x = 0, y = 8}, [6] = {x = 7, y = 19}, [7] = {x = 117, y = 19}, [8] = {x = 140, y =-4}, [9] = {x = 140, y =-15},
    [10] = {x = 140, y = -15}, [11] = {x = 119, y = -15}, [12] = {x = 140, y = 4}, [13] = {x = 140, y = 19},
    [14] = {x = 140, y = 19}, [15] = {x = 118, y = 19}, [16] = {x = 65, y = -16}, [17] = {x = 45, y = -35},
    [18] = {x = 45, y = -35},[19] = {x = 20, y = -35}, [20] = {x = 20, y = -15}, [21] = {x = 20, y = -35}}
  
  for i = 1, #enemy.vectorR, 1 do
    enemy.vectorR[i].x = enemy.vectorR[i].x*screen.scale
    enemy.vectorR[i].y = enemy.vectorR[i].y*screen.scale
  end

 for i = 1, #enemy.vectorL, 1 do
    enemy.vectorL[i].x = enemy.vectorL[i].x*screen.scale
    enemy.vectorL[i].y = enemy.vectorL[i].y*screen.scale
  end
end
--
function rayCall(fixture, x, y, xn, yn, fraction)
  if fraction > 0.8 then
    enemy.fire = true
  end
  
  return 1
end

--
function enemy:update(dt)
  --Raycast to player
  if math.abs(player.pos.x - enemy.pos.x) < 400 and math.abs(player.pos.y - enemy.pos.y) < 400 then
    world:rayCast( enemy.pos.x - grid/2, enemy.pos.y- grid/2, player.pos.x- grid/2, player.pos.y- grid/2, rayCall)
  end
  
  --Enemy health
  if enemy.health <= 0 then
    enemy.dead = true
  end
  enemy.pos.x = enemy.body:getX()
  enemy.pos.y = enemy.body:getY()
  --Clamp the enemy's grid position to the nearest grid space
  enemy.pos.gx = math.floor((enemy.pos.x+grid/2)/grid-1)
  enemy.pos.gy = math.floor((enemy.pos.y+grid/2)/grid - 1)
  
  --Acceleration
  if not enemy.dead then
    if enemy.move.u then
      enemy.body:applyForce(0,-1000)
    elseif enemy.move.d then
      enemy.body:applyForce(0,1000)
    end
    if enemy.move.l then
      enemy.body:applyForce(-1000,0)
    elseif enemy.move.r then
      enemy.body:applyForce(1000,0)
    end
    --Clamp the player's velocity to +- player.maxVel
    local vx,vy = enemy.body:getLinearVelocity()
    vx = math.min(math.max(-enemy.maxVel, vx), enemy.maxVel)
    vy = math.min(math.max(-enemy.maxVel, vy), enemy.maxVel)
    enemy.body:setLinearVelocity(vx,vy)
  else
    enemy.body:applyForce(0,1000)
  end
  
  --Reload missiles
  if not enemy.canFire then
    if enemy.reload >= 3 then
      enemy.canFire = true
    else  
      enemy.reload = enemy.reload + dt
    end
  end
    --Update missiles
  for i = 1, #enemy.missiles, 1 do
    enemy.missiles[i].anim:update(dt)
    local x,y = enemy.missiles[i].body:getLinearVelocity()
    --If the velocity of the missile has changed, explode.
    if (x ~= enemy.missiles[i].px or y ~= enemy.missiles[i].py) and not enemy.missiles[i].drawExp then
      enemy.missiles[i].ex, enemy.missiles[i].ey = enemy.missiles[i].body:getPosition()
      enemy.missiles[i].drawExp = true
      enemy.missiles[i].anim:gotoFrame(1)
      enemy.missiles[i].anim:resume()
      enemy.missiles[i].timer = 0
      --Remove physics fixture from missile so it can't collide a second time
      enemy.missiles[i].fixture:destroy()
    end
    
    if enemy.missiles[i].drawExp then
      enemy.missiles[i].timer = enemy.missiles[i].timer + dt
      if enemy.missiles[i].timer >= 1 then
        table.remove(enemy.missiles[i])
      end
    end
    
  end
  
  --When to fire a missile
  if enemy.canFire and not enemy.dead then
    if enemy.fire then
      local angle = math.atan2(player.pos.y - enemy.pos.y, player.pos.x - enemy.pos.x)
      local m = enemy.missiles
      local i = #m + 1
      m[i] = {}
      m[i].body = love.physics.newBody(world, enemy.pos.x,enemy.pos.y, 'dynamic')
      m[i].shape = love.physics.newRectangleShape(0,0,10,1)
      m[i].fixture = love.physics.newFixture(m[i].body,m[i].shape)
      m[i].body:setAngle(angle)
      m[i].body:setLinearDamping(0)
      m[i].fixture:setRestitution(0)
      m[i].fixture:setCategory(7)
      m[i].fixture:setMask(3,2)
      m[i].body:applyLinearImpulse(math.cos(angle)*20,math.sin(angle)*20)
      local g = anim8.newGrid(64,64, explosion.i:getWidth(), explosion.i:getHeight())
      m[i].anim = anim8.newAnimation(g('1-6',1), 0.05, 'pauseAtEnd')
      m[i].drawExp = false
      m[i].px, m[i].py = m[i].body:getLinearVelocity()
      m[i].ex, m[i].ey = 0,0
      m[i].explode = love.audio.newSource(enemy.expMissile, 'static')
      
      enemy.reload = 0
      enemy.canFire = false
      enemy.fire = false
    end
    
    end
end

--
function enemy:draw()

  --Draw the enemy sub
  love.graphics.setColor(255,00,0,255)
  if enemy.drawL then
    --love.graphics.draw(enemy.image, enemy.pos.x - 12, enemy.pos.y + enemy.iOff.y,0,-1,1)
    drawEnemy('left')
  else
    --love.graphics.draw(enemy.image, enemy.pos.x + enemy.iOff.x, enemy.pos.y + enemy.iOff.y,0,1,1)
    drawEnemy('right')
  end
  
  --Missiles
  for i = 1, #enemy.missiles, 1 do
    if not enemy.missiles[i].drawExp then
    love.graphics.setColor(255,255,0,255)
    love.graphics.draw(enemy.mi, enemy.missiles[i].body:getX() - grid/2, enemy.missiles[i].body:getY() - grid/2, enemy.missiles[i].body:getAngle(),0.5,1)
    else
      love.graphics.setColor(255,128,0,255)
      enemy.missiles[i].anim:draw(explosion.i, enemy.missiles[i].ex - explosion.i:getWidth()/6*1.5, enemy.missiles[i].ey - explosion.i:getHeight()*1.5)
    end
    
  end
end
--
function drawEnemy(dir)
  local x,y = enemy.pos.x, enemy.pos.y
  local sx,sy = x-grid*.9, y-grid/2
  if dir == 'right' then
    for i = 1, #enemy.vectorR, 1 do
      --Draw the sub body
      if i <= 6 then
        local r1 = enemy.vectorR[i]
        local r2 = enemy.vectorR[i+1]
        love.graphics.line(sx + r1.x, sy + r1.y, sx + r2.x, sy + r2.y)
        if i == 6 then
          love.graphics.line(sx + r2.x, sy + r2.y, sx + enemy.vectorR[1].x, sy + enemy.vectorR[1].y)
        end
      else
        --Draw fins and hatch
        if i%2 == 0 then
          local r1 = enemy.vectorR[i]
          local r2 = enemy.vectorR[i+1]
          love.graphics.line(sx+r1.x,sy+r1.y, sx+r2.x, sy+r2.y)
        end
      end
    end
  else
    for i = 1, #enemy.vectorL, 1 do
      --Draw the sub body
      if i <= 6 then
        local r1 = enemy.vectorL[i]
        local r2 = enemy.vectorL[i+1]
        love.graphics.line(sx + r1.x, sy + r1.y, sx + r2.x, sy + r2.y)
        if i == 6 then
          love.graphics.line(sx + r2.x, sy + r2.y, sx + enemy.vectorL[1].x, sy + enemy.vectorL[1].y)
        end
      else
        --Draw fins and hatch
        if i%2 == 0 then
          local r1 = enemy.vectorL[i]
          local r2 = enemy.vectorL[i+1]
          love.graphics.line(sx+r1.x,sy+r1.y, sx+r2.x, sy+r2.y)
        end
      end
    end
  end

end

